package com.cykj.shop.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cykj.common.core.constant.SecurityConstants;
import com.cykj.common.core.domain.R;
import com.cykj.common.core.utils.JwtUtils;
import com.cykj.common.core.utils.poi.ExcelUtil;
import com.cykj.common.core.web.controller.BaseController;
import com.cykj.common.core.web.domain.AjaxResult;
import com.cykj.common.core.web.page.TableDataInfo;
import com.cykj.common.log.annotation.Log;
import com.cykj.common.log.enums.BusinessType;
import com.cykj.common.redis.service.RedisService;
import com.cykj.common.security.annotation.RequiresPermissions;
import com.cykj.system.api.model.BsLoginUser;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.system.api.domain.BsUser;
import com.cykj.shop.service.IBsUserService;

/**
 * 商家用户Controller
 *
 * @author 天才少年
 * @date 2025-08-21
 */
@RestController
@RequestMapping("/bsUser")
public class BsUserController extends BaseController {
    @Autowired
    private IBsUserService bsUserService;


    @Autowired
    private RedisService redisService;

    /**
     * @Description:根据手机号获取商家信息
     * @Param:
     * @Return:
     */
    @GetMapping("/getUser/{phone}")
    public R<BsLoginUser> getShopUserInfoByName(@PathVariable("phone") String phone) {

        BsUser bsUser = bsUserService.getShopUserInfoByName(phone);

        BsLoginUser bsLoginUser = new BsLoginUser();
        bsLoginUser.setBsUser(bsUser);
        return R.ok(bsLoginUser);
    }


    /**
    * @Description:通过token获取用户信息
    * @Param:[request]
    * @Return:com.cykj.common.core.web.domain.AjaxResult
    */
    @GetMapping("/getInfo")
    public AjaxResult getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseToken(token);

        String userKey = "login_tokens:" + JwtUtils.getUserKey(claims);

        BsLoginUser bsLoginUser = redisService.getCacheObject(userKey);

        BsUser bsUser = bsLoginUser.getBsUser();

        Long userID = bsUser.getId();
        String username = bsUser.getName();
        String avatar = bsUser.getAvatar();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("userID", userID);
        hashMap.put("username", username);
        hashMap.put("avatar", avatar);
        return success(hashMap);
    }


    /**
     * 查询商家用户列表
     */
    @RequiresPermissions("shop:bsUser:list")
    @GetMapping("/list")
    public TableDataInfo list(BsUser bsUser) {
        startPage();
        List<BsUser> list = bsUserService.selectBsUserList(bsUser);
        return getDataTable(list);
    }

    /**
     * 导出商家用户列表
     */
    @RequiresPermissions("shop:bsUser:export")
    @Log(title = "商家用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsUser bsUser) {
        List<BsUser> list = bsUserService.selectBsUserList(bsUser);
        ExcelUtil<BsUser> util = new ExcelUtil<BsUser>(BsUser.class);
        util.exportExcel(response, list, "商家用户数据");
    }

    /**
     * 获取商家用户详细信息
     */
    @RequiresPermissions("shop:bsUser:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bsUserService.selectBsUserById(id));
    }

    /**
     * 新增商家用户
     */
    @RequiresPermissions("shop:bsUser:add")
    @Log(title = "商家用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsUser bsUser) {
        return toAjax(bsUserService.insertBsUser(bsUser));
    }

    /**
     * 修改商家用户
     */
    @RequiresPermissions("shop:bsUser:edit")
    @Log(title = "商家用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsUser bsUser) {
        return toAjax(bsUserService.updateBsUser(bsUser));
    }

    /**
     * 删除商家用户
     */
    @RequiresPermissions("shop:bsUser:remove")
    @Log(title = "商家用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bsUserService.deleteBsUserByIds(ids));
    }
}
